<?php

include_once 'Hasher.php';
include_once 'model/exceptions/UserExistsException.php';
include_once 'model/User.php';
include_once 'model/UserPageDAO.php';

class UserDAO {

	private static $instance;
	private $users, $usersID;
	
	private function __construct() {
	}

	public static function getInstance() {
		if (!isset(self::$instance)) {
			$className = __CLASS__;
			self::$instance = new $className;
		}
		return self::$instance;
	}
	
	public function getUser($login) {		
//		if(isset($this->users[$login])) {
//			return $this->users[$login];
//		}
		$user = new User();
		$db_conn = DBConnection::getInstance();
		$result = NULL;
		try{		
			$result = $db_conn->exec_select_query($user->getUserStatement(),array($login));		
		}catch (PDOException $e) {
			echo "Error: ".$e->getMessage().'<br />';					
		}		
		if (!$result) {
			return NULL;			
		}		
//		$this->users[$login] = $this->fillUserStructure($result);
//		$this->usersID[$user->getId()] = &$this->users[$login];
		$user=$this->fillUserStructure($result);
//		return $this->users[$login];
		return $user;
	}
	
	public function getUserById($id) {
//		if(isset($this->usersID[$id]))
//			return $this->usersID[$id];
		$user=new User();
		$db_conn=DBConnection::getInstance();
		$result=null;
		try{		
			$result = $db_conn->exec_select_query($user->getUserByIdStatement(),array($id));
		}catch (PDOException $e) {
			echo "Error: ".$e->getMessage().'<br />';				
		}		
		if (!$result) {
			return NULL;			
		}
//		$this->users[$user->getLogin()] = $this->fillUserStructure($result);
//		$this->usersID[$id] = &$this->users[$user->getLogin()];
//		return $this->users[$user->getLogin()];
		$user=$this->fillUserStructure($result);
		return $user;
	}
	
	public function addUser($user) {
		$tmp_user=$this->getUser($user->getLogin());
		if ($tmp_user) {
			throw new UserExistsException('User <b>'.$user->getLogin().'</b> already exists!');
		}
		//echo '<h1> Add user:'.$user->getLogin().'</h1><br />';
		try {
			$db_conn = DBConnection::getInstance();
			$dbh = $db_conn->getDBH();
			$smt = $dbh->prepare($user->getAddUserStatement());
			$smt->execute(array($user->getLogin(), $user->getPassHash(),$user->getMail(),$user->getAvatar()));
			$uid = $dbh->lastInsertId();
			UserPageDAO::getInstance()->addDefaultUserPage($uid, $user->getLogin());
		}catch(PDOException $e) {
			echo 'Error: '.$e->getMesage().'<br />';
		}
	}
	
	public function getCountUsers() {
		return DBConnection::getInstance()->exec_count_select("SELECT * FROM user");
	}
	
	private function fillUserStructure($result) {
		$user=new User();		
		$user->setId($result[0]["id"]);
		$user->setAvatar($result[0]["avatar"]);
		$user->setLogin($result[0]["login"]);
		$user->setMail($result[0]["mail"]);
		$user->setPassHash($result[0]["password"]);
		return $user;
	}
	
	public function changePassword($id, $pass) {
		$db=DBConnection::getInstance();
		if($db==null)
			return;
		try{			
			$db->exec_param_transaction(User::getChangePasswordStatement(),array(Hasher::hash($pass),$id));
		}catch(PDOException $e) {
			echo 'Error: '.$e->getMessage().'<br />'; 
		}
	}
	
	public function changeAvatar($id, $avat) {
		$db=DBConnection::getInstance();
		if($db==null)
			return;
		try{						
			$db->exec_param_transaction(User::getChangeAvatarStatement(),array($avat,$id));
		}catch(PDOException $e) {
			echo 'Error: '.$e->getMessage().'<br />'; 
		}
	}
}

?>